<?php
require_once dirname(__FILE__) . '/common.inc.php';
require_once dirname(__FILE__) . "/db/$database/database.inc.php";

header('Content-Type: text/plain');

$query = json_decode(MiscUtils::getParam('q', '[]'));
$id = $query->i;

try {
  $hostname = DB_HOST;
  $username = DB_LOGIN;
  $password = DB_PASS;
  $dbname = DB_NAME;
  $myPdo = new PDO ('mysql:host=' . $hostname, $username, $password);
  $myPdo->exec('USE ' . $dbname);
  $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
  echo ERROR_SIGN . $e->getMessage();
}
$result = MiscUtils::db($myPdo, '*', 'pz_file', NULL, "id = $id", NULL, NULL, NULL, NULL);
if (count($result->data) != 1) {
  echo ERROR_SIGN . 'Not Found In Database';
  exit;
}
$object = $result->data[0];

if (!file_exists($object->link)) {
  echo ERROR_SIGN . 'Not Found In Hard Drive';
  exit;
} else {
  $file = fopen($object->link, "r");
  //输入文件标签   
  Header("Content-type: application/octet-stream");
  Header("Accept-Ranges: bytes");
  Header("Accept-Length:" . filesize($object->link));
  Header("Content-Disposition: attachment; filename=\"" . $object->name . "\"");
  //输出文件内容   
  echo fread($file, filesize($object->link));
  fclose($file);
  exit;
}
?>